package org.cttic.visualization.visualization.util.itineraryQuery;

import org.cttic.visualization.visualization.common.EsDataStrip;
import org.cttic.visualization.visualization.common.itineraryQueryData;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.provinceUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-25 16:59
 */
@Component
public class queryUtil {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public SearchHit[] queryByTravelId(String vehicleNo,String vehicleColor,String travelId,String provinceId){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleNo",vehicleNo));
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleColor",vehicleColor));
        boolQueryBuilder.must(QueryBuilders.termQuery("travelId",travelId));
        if(provinceId!=null&&!provinceId.contains("|")){
            System.out.println(Long.parseLong(provinceId)*10000+"");
            System.out.println(provinceId+9999);
            boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(provinceId)*10000+"").lte(provinceId+9999));
        }else if(provinceId!=null&&provinceId.contains("|")){
            String[] split = provinceId.split("\\|");
            for (int i=0;i<split.length;i++){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(split[i])*10000+"").lte(split[i]+9999));
            }
        }
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));

        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.sort("tansTime");
        searchSourceBuilder.size(100);

        String month = DateUtil.StampToMonth(System.currentTimeMillis());
        // 执行搜索,向ES发起http请求
        SearchRequest Request = new SearchRequest("trip_info_"+month);
        Request.types("_doc");

        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {

            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);
        SearchHit[] hits = rp.getHits().getHits();
        return hits;
    }
    public SearchHit[] queryByTravelId2(String vehicleNo,String vehicleColor,String travelId,String provinceId){
        org.cttic.visualization.visualization.common.itineraryQueryData.Trip trip = new itineraryQueryData.Trip();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleNo",vehicleNo));
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleColor",vehicleColor));
        boolQueryBuilder.must(QueryBuilders.termQuery("travelId",travelId));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));
        if(provinceId!=null&&!provinceId.contains("|")){
            System.out.println(Long.parseLong(provinceId)*10000+"");
            System.out.println(provinceId+9999);
            boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(provinceId)*10000+"").lte(provinceId+9999));
        }else if(provinceId!=null&&provinceId.contains("|")){
            String[] split = provinceId.split("\\|");
            for (int i=0;i<split.length;i++){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(split[i])*10000+"").lte(split[i]+9999));
            }
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.sort("tansTime");
        searchSourceBuilder.size(100);
        String month = DateUtil.StampToMonth(System.currentTimeMillis());
        // 执行搜索,向ES发起http请求
        SearchRequest Request = new SearchRequest("trip_incomplete_"+month,"trip_incomplete_202005");
        Request.types("_doc");

        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {

            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);
        SearchHit[] hits = rp.getHits().getHits();
        return hits;
    }
    public SearchHit[] queryByTansTimeBetween(String vehicleNo,String vehicleColor,String tansTime1,String tansTime2,String provinceId){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleNo",vehicleNo));
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleColor",vehicleColor));
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime").gte(tansTime1).lte(tansTime2));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));
        if(provinceId!=null&&!provinceId.contains("|")){
            System.out.println(Long.parseLong(provinceId)*10000);
            System.out.println(provinceId+9999);
            boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(provinceId)*10000+"").lte(provinceId+9999));
        }else if(provinceId!=null&&provinceId.contains("|")){
            String[] split = provinceId.split("\\|");
            for (int i=0;i<split.length;i++){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(split[i])*10000+"").lte(split[i]+9999));
            }
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.sort("tansTime");
        searchSourceBuilder.size(1000);
        String month = DateUtil.StampToMonth(System.currentTimeMillis());
        // 执行搜索,向ES发起http请求
        SearchRequest Request = new SearchRequest("trip_info_"+month);
        Request.types("_doc");
        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {

            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);
        SearchHit[] hits = rp.getHits().getHits();
        return hits;
    }
    public SearchHit[] queryByTansTimeBetween2(String vehicleNo,String vehicleColor,String tansTime1,String tansTime2,String provinceId){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleNo",vehicleNo));
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleColor",vehicleColor));
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime").gte(tansTime1).lte(tansTime2));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));
        if(provinceId!=null&&!provinceId.contains("|")){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(provinceId)*10000+"").lte(provinceId+9999));
        }else if(provinceId!=null&&provinceId.contains("|")){
            String[] split = provinceId.split("\\|");
            for (int i=0;i<split.length;i++){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(split[i])*10000+"").lte(split[i]+9999));
            }
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.sort("tansTime");
        searchSourceBuilder.size(1000);
        String month = DateUtil.StampToMonth(System.currentTimeMillis());
        // 执行搜索,向ES发起http请求
        SearchRequest Request = new SearchRequest("trip_incomplete_"+month,"trip_incomplete_202005");
        Request.types("_doc");
        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {

            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);
        SearchHit[] hits = rp.getHits().getHits();
        return hits;
    }
    public boolean judgeProvinceId(String vehicleNo,String vehicleColor,String provinceId){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleNo",vehicleNo));
        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleColor",vehicleColor));
        boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gte(Long.parseLong(provinceId)*10000+"").lte(provinceId+9999));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.sort("tansTime");
        searchSourceBuilder.size(10);
        String month = DateUtil.StampToMonth(System.currentTimeMillis());
        // 执行搜索,向ES发起http请求
        SearchRequest Request = new SearchRequest("trip_info_"+month,"trip_info_202005","trip_incomplete_202005","trip_incomplete_"+month);
        Request.types("_doc");
        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {

            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);
        SearchHit[] hits = rp.getHits().getHits();
        if(hits.length==0) {
            return false;
        }else {
            return true;
        }

    }
}
